package com.ruoyi.quanzi.controller.app;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunPost;
import com.ruoyi.quanzi.service.ISunPostService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.github.pagehelper.PageHelper;

/**
 * 移动端帖子Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/app/post")
public class AppPostController extends BaseController
{
    @Autowired
    private ISunPostService sunPostService;

    /**
     * 查询帖子列表
     */
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long topicId,
            @RequestParam(defaultValue = "new") String orderBy)
    {
        // 手动设置分页参数
        PageHelper.startPage(page, pageSize);
        
        if (topicId != null) {
            List<SunPost> list = sunPostService.selectPostsByTopic(topicId, orderBy, null);
            return getDataTable(list);
        } else {
            SunPost sunPost = new SunPost();
            List<SunPost> list = sunPostService.selectSunPostList(sunPost);
            return getDataTable(list);
        }
    }

    /**
     * 查询热门帖子
     */
    @GetMapping("/hot")
    public AjaxResult hot(@RequestParam(defaultValue = "10") Integer limit)
    {
        List<SunPost> list = sunPostService.selectHotPosts(limit);
        return success(list);
    }

    /**
     * 查询推荐帖子
     */
    @GetMapping("/recommend")
    public AjaxResult recommend(@RequestParam(defaultValue = "10") Integer limit)
    {
        Long uid = getUserId(); // 获取当前用户ID
        List<SunPost> list = sunPostService.selectRecommendPosts(uid, limit);
        return success(list);
    }

    /**
     * 获取帖子详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        // 增加阅读数
        sunPostService.updatePostReadCount(id);
        
        // 获取帖子详情
        Long uid = getUserId(); // 获取当前用户ID
        SunPost post = sunPostService.getPostDetail(id, uid);
        return success(post);
    }

    /**
     * 发布帖子
     */
    @Log(title = "发布帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SunPost sunPost)
    {
        // 设置当前登录用户ID
        sunPost.setUid(getUserId());
        sunPost.setCreateBy(getUsername());
        
        return toAjax(sunPostService.insertSunPost(sunPost));
    }

    /**
     * 修改帖子
     */
    @Log(title = "修改帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SunPost sunPost)
    {
        // 设置当前登录用户ID
        sunPost.setUpdateBy(getUsername());
        
        return toAjax(sunPostService.updateSunPost(sunPost));
    }

    /**
     * 删除帖子
     */
    @Log(title = "删除帖子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(sunPostService.deleteSunPostById(id));
    }

    /**
     * 点赞帖子
     */
    @PostMapping("/{id}/like")
    public AjaxResult like(@PathVariable Long id)
    {
        // 获取当前用户ID
        Long uid = getUserId();
        
        // 这里需要实现点赞逻辑，可能需要创建点赞记录表
        // 暂时只更新点赞数
        SunPost post = sunPostService.selectSunPostById(id);
        if (post != null) {
            sunPostService.updatePostLikeCount(id, (post.getLikeCount() != null ? post.getLikeCount() : 0) + 1);
            return success("点赞成功");
        }
        return error("帖子不存在");
    }

    /**
     * 取消点赞
     */
    @PostMapping("/{id}/unlike")
    public AjaxResult unlike(@PathVariable Long id)
    {
        // 这里需要实现取消点赞逻辑
        SunPost post = sunPostService.selectSunPostById(id);
        if (post != null) {
            int likeCount = Math.max(0, (post.getLikeCount() != null ? post.getLikeCount() : 0) - 1);
            sunPostService.updatePostLikeCount(id, likeCount);
            return success("取消点赞成功");
        }
        return error("帖子不存在");
    }

    /**
     * 收藏帖子
     */
    @PostMapping("/{id}/collect")
    public AjaxResult collect(@PathVariable Long id)
    {
        // 获取当前用户ID
        Long uid = getUserId();
        
        // 这里需要实现收藏逻辑，可能需要创建收藏记录表
        SunPost post = sunPostService.selectSunPostById(id);
        if (post != null) {
            sunPostService.updatePostCollectCount(id, (post.getCollectCount() != null ? post.getCollectCount() : 0) + 1);
            return success("收藏成功");
        }
        return error("帖子不存在");
    }

    /**
     * 取消收藏
     */
    @PostMapping("/{id}/uncollect")
    public AjaxResult uncollect(@PathVariable Long id)
    {
        // 这里需要实现取消收藏逻辑
        SunPost post = sunPostService.selectSunPostById(id);
        if (post != null) {
            int collectCount = Math.max(0, (post.getCollectCount() != null ? post.getCollectCount() : 0) - 1);
            sunPostService.updatePostCollectCount(id, collectCount);
            return success("取消收藏成功");
        }
        return error("帖子不存在");
    }

    /**
     * 分享帖子
     */
    @PostMapping("/{id}/share")
    public AjaxResult share(@PathVariable Long id)
    {
        sunPostService.updatePostShareCount(id);
        return success("分享成功");
    }

    /**
     * 搜索帖子
     */
    @GetMapping("/search")
    public TableDataInfo search(
            @RequestParam String keyword,
            @RequestParam(required = false) Long topicId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize)
    {
        // 手动设置分页参数
        PageHelper.startPage(page, pageSize);
        
        List<SunPost> list = sunPostService.searchPosts(keyword, topicId, null);
        return getDataTable(list);
    }

    /**
     * 获取用户发布的帖子
     */
    @GetMapping("/user/{uid}")
    public TableDataInfo userPosts(
            @PathVariable Long uid,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize)
    {
        // 手动设置分页参数
        PageHelper.startPage(page, pageSize);
        
        List<SunPost> list = sunPostService.selectPostsByUser(uid, 1, null); // 1表示正常状态
        return getDataTable(list);
    }

    /**
     * 获取用户收藏的帖子
     */
    @GetMapping("/user/collections")
    public TableDataInfo userCollections(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize)
    {
        // 手动设置分页参数
        PageHelper.startPage(page, pageSize);
        
        Long uid = getUserId(); // 获取当前用户ID
        List<SunPost> list = sunPostService.selectCollectedPosts(uid, null);
        return getDataTable(list);
    }

    /**
     * 获取用户点赞的帖子
     */
    @GetMapping("/user/likes")
    public TableDataInfo userLikes(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize)
    {
        // 手动设置分页参数
        PageHelper.startPage(page, pageSize);
        
        Long uid = getUserId(); // 获取当前用户ID
        List<SunPost> list = sunPostService.selectLikedPosts(uid, null);
        return getDataTable(list);
    }
}